import { Modal, Form, Button, Input, Select } from 'antd'
import React from 'react'
import { routerRedux } from 'dva/router'
import pathToRegexp from 'path-to-regexp'

const FormItem = Form.Item

const doCancel = ({
                    visible,
                    dispatch,
                    currentItem,
                    form: {
                      getFieldDecorator,
                      validateFieldsAndScroll,
                      getFieldValue,
                      setFieldsValue
                    }
                  }) => {
  const handleCancel = () => {
    dispatch({
      type: 'orderCancel/hideModal',
      payload: {}
    })
  }
  const handleOk = (e) => {
    e.preventDefault()
    validateFieldsAndScroll((err, values) => {
      if (!err) {
        dispatch({
          type: 'orderCancel/create',
          payload: values
        })
        setTimeout(() => {
          const pathname = location.pathname;
          const match = pathToRegexp('/order/updateContractOrder/:id').exec(pathname);
          if (match) {
            let orderId = match[1];
            dispatch({type: 'updateContractOrder/getOrderDetail', payload: { orderId }})
          }

        }, 1000)
      }
    })
  }

  return (
    <Form>
      <Modal
        visible={visible}
        title="合同取消"
        onOk={handleOk}
        onCancel={handleCancel}
        width={300}
        destroyOnClose={true}
        footer={[
          <Button key="back" onClick={handleCancel}>取消</Button>,
          <Button key="submit" type="primary" htmlType="submit" onClick={handleOk}>
            确认
          </Button>,
        ]}>
        {getFieldDecorator('contractId', {
          initialValue: currentItem.id,
        })(
          <Input type='hidden'/>
        )}
        {getFieldDecorator('contractVersion', {
          initialValue: currentItem.version,
        })(
          <Input type='hidden'/>
        )}
        <FormItem
          label="取消理由"
          labelCol={{span: 7}}
          wrapperCol={{span: 15}}
        >
          {getFieldDecorator('cancelReason', {
            rules: [
              {required: true, message: '请输入取消理由'},
              {whitespace: true}
            ],
            initialValue: '',
            validateTrigger: 'onSubmit'
          })(
            <Input/>
          )}
        </FormItem>
      </Modal>
    </Form>
  )
}
export default Form.create()(doCancel)
